package com.chance.cc.crawler.development.bootstrap.du;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.downloader.http.HttpClientDownloader;
import com.chance.cc.crawler.core.downloader.http.HttpClientGenerator;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.downloader.proxy.SiteConfigProxyProvider;
import com.chance.cc.crawler.core.selector.Json;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.util.*;

import static com.chance.cc.crawler.core.script.crawler.CrawlerScript.$_type;
import static org.codehaus.groovy.runtime.EncodingGroovyMethods.md5;

/**
 * @author lt
 * @version 1.0
 * @date 2021-04-27 11:00:35
 * @email okprog@sina.com
 */
public class DUSellCalenderTest {
    public static Logger log = LoggerFactory.getLogger(DUSellCalenderTest.class);

    private static Proxy proxy = new Proxy();

    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HL89Q19E86E2987D");
        proxy.setPassword("71F33D94CE5F7BF2");
    }

    public static void main(String[] args) throws Exception {

//        String url = "https://m.dewu.com/sns/v1/sell/index?v=4.2.0&newSign=01c0b8d1c323d1cd4ee0596f7aab541d";
////        String url = "https://m.dewu.com/sns/v1/sell/list?sellTime=202105&startDate=&endDate=&categoryId=&lastId=&v=4.2.0&newSign=130eba44aafaa8282c00ccd305721eff";
//
//        List<NameValuePair> parameters = URLEncodedUtils.parse(url.split("\\?")[1], Charset.forName("utf-8"));
//        Map<String,Object> params = new HashMap<>();
//        for (NameValuePair parameter : parameters) {
//            params.put(parameter.getName(),parameter.getValue());
//        }
//
//        long l = System.currentTimeMillis();
//        System.out.println(l);
//        params.put("uuid","c3e92d44fe7c3f9e");
//        params.put("platform","android");
//        params.put("v","4.68.6");
//        params.put("loginToken","");
//        params.put("timestamp",String.valueOf(l));//header中的时间戳
//        String s = genSignature(params,"d245a0ba8d678a61");
//        System.out.println("sign:   " + s);


        HttpConfig httpConfig = HttpConfig.me("test");
        httpConfig.setProxy(proxy);
        httpConfig.setUseProxy(true);

        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());

        for (int i = 1; i <= 1; i++) {
            new Thread(new TestSearchCommentRun(downloader, httpConfig), "thread_" + i).start();
        }

    }
    public static String genSignature(Map<String,Object> params) {
        return genSignature(params,"19bc545a393a25177083d4a748807cc0");
    }

    public static String genSignature(Map<String,Object> params,String salt) {
        if(params == null){
            return null;
        }
        String sign = "";
        StringBuffer sb = new StringBuffer();
        try {
            // 1. 字典升序排序
            SortedMap<String,Object> sortedMap = new TreeMap<>(params);
            // 2. 拼按URL键值对
            Set<String> keySet = sortedMap.keySet();
            for(String key : keySet){
                //sign不参与算法
                if(key.equals("sign")){
                    continue;
                }

                Object obj =  sortedMap.get(key);
                String value = obj.toString() ;
                if (obj instanceof Map){
                    value = JSON.toJSONString(obj);
                }
                if (obj instanceof List){
                    List list = (List) obj;
                    value = "";
                    for (Object o : list) {
                        if (value.length() > 0){
                            value += ",";
                        }
                        value += JSON.toJSONString(o);
                    }
//                    value = ","+JSON.toJSONString(obj);
                }
//                if ("[]".equals(value)){
//                    value = "";
//                }
                sb.append(key).append(value);

            }
            String uriString = sb.toString() + salt;
            // 3. MD5运算得到请求签名
            sign = md5(uriString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sign;
    }


    static class TestSearchCommentRun implements Runnable {

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        Set<String> spuids = new HashSet<>();
        static String seriesBrandUrl = "https://app.dewu.com/api/v1/app/search/ice/search/detail_brand";

        public TestSearchCommentRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {

            String page = "0";
            int count = 0;
            while (true) {
                log.info("采集当前页为：{}",count);
//                if (count > 255){
//                    break;
//                }

                count ++;

//                List<NameValuePair> parameters = URLEncodedUtils.parse(seriesBrandUrl.split("\\?")[1], Charset.forName("utf-8"));
//                Map<String,Object> params = new HashMap<>();
//                for (NameValuePair parameter : parameters) {
//                    params.put(parameter.getName(),parameter.getValue());
//                }
                long l = System.currentTimeMillis();
//                params.put("uuid","c3e92d44fe7c3f9e");
//                params.put("platform","android");
//                params.put("v","4.68.6");
//                params.put("loginToken","");
//                params.put("timestamp",String.valueOf(l));//header中的时间戳
//                String sign = genSignature(params,"d245a0ba8d678a61");


                String url = "https://app.dewu.com/api/v1/h5/index/fire/flow/product/detail";

                HttpRequest httpRequest = new HttpRequest();
                httpRequest.setUrl(url);
                httpRequest.addHeader("appid","wxapp");
                httpRequest.addHeader("content-type","application/json");
//                httpRequest.addHeader("duuuid","c3e92d44fe7c3f9e");
//                httpRequest.addHeader("duplatform","android");
//                httpRequest.addHeader("duv","4.68.6");
//                httpRequest.addHeader("loginToken","");
//                httpRequest.addHeader("timestamp", String.valueOf(l));
//                httpRequest.addHeader("Host","m.dewu.com");
                httpRequest.setMethod("POST");
                Map<String,Object> params = new HashMap<>();
                params.put("spuId","25468");
                params.put("productSourceName","sellDetail");
                params.put("propertyValueId","0");

                String sign = genSignature(params);
                params.put("sign",sign);
                HttpRequestBody httpRequestBody = HttpRequestBody.json(JSON.toJSONString(params), "utf-8");
                httpRequest.setRequestBody(httpRequestBody);

                httpRequest.addHeader("Host","app.dewu.com");
                httpRequest.addHeader("Connection","keep-alive");
                httpRequest.addHeader("User-Agent",genRandomUA());
                httpRequest.addHeader("Content-Type","application/json");
                httpRequest.addHeader("platform","h5");
                httpRequest.addHeader("AppId","wxapp");
                httpRequest.addHeader("appVersion","4.4.0");
                httpRequest.addHeader("charset","utf-8");
                httpRequest.addHeader("Accept-Encoding","gzip,compress,br,deflate");
                httpRequest.addHeader("Referer","https://servicewechat.com/wx3c12cdd0ae8b1a7b/225/page-frame.html");
                httpRequest.addHeader("wxapp-login-token","");


                HttpPage download = null;
                try {
                    download = downloader.download(httpRequest, httpConfig);
                    Json json = new Json(download.getRawText());
                    List<String> all = json.jsonPath($_type + ".data.productList").all();
                    if (null == all){
                        break;
                    }
//                    page = json.jsonPath($_type + ".data.lastId").get();
                    for (String item : all) {
                        Json itemJson = new Json(item);
                        String spuId = itemJson.jsonPath($_type + ".spuId").get();
                        String title = itemJson.jsonPath($_type + ".title").get();

                        if (!spuids.contains(spuId)){
                            spuids.add(spuId);
                            continue;
                        }

                        log.warn("重复记录：{}，page：{}",spuId+"-->"+title,page);
                    }
                } catch (Exception e) {
//                   continue;
                }

            }

            for (String spuid : spuids) {
                log.info("采集记录：{}",spuid);
            }

            log.info("采集总量：{}",spuids.size());
        }

    }

    private static String genRandomUA(){
        int part1 = RandomUtils.nextInt(1000, 9999);
        int part2 = RandomUtils.nextInt(1000, 1999);
        int part3 = RandomUtils.nextInt(100, 999);

        return String.format("Mozilla/5.0 (Linux; Android 8.0.0; MI 5s Build/OPR1.170623.032; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2797 MMWEBSDK/20210501 Mobile Safari/537.36 MMWEBID/%s MicroMessenger/8.0.6.%s(0x28000%s) Process/appbrand0 WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64 MiniProgramEnv/android",
                part1,part2,part3);
    }
}



class AESUtils {
    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "AES";
    /**
     * 加解密算法/工作模式/填充方式
     */
    private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";

    /**
     * SecretKeySpec类是KeySpec接口的实现类,用于构建秘密密钥规范
     */
    private SecretKeySpec key;

    public AESUtils(String hexKey) {
        key = new SecretKeySpec(hexKey.getBytes(), ALGORITHM);
    }

    /**
     * AES加密
     * @param data
     * @return
     * @throws Exception
     */
    public String encryptData(String data) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_STR); // 创建密码器
        cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
        return new BASE64Encoder().encode(cipher.doFinal(data.getBytes()));
    }

    /**
     * AES解密
     * @param base64Data
     * @return
     * @throws Exception
     */
    public String decryptData(String base64Data) throws Exception{
        Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(new BASE64Decoder().decodeBuffer(base64Data)));
    }

    /**
     * hex字符串 转 byte数组
     * @param s
     * @return
     */
    private static byte[] hex2byte(String s) {
        if (s.length() % 2 == 0) {
            return hex2byte (s.getBytes(), 0, s.length() >> 1);
        } else {
            return hex2byte("0"+s);
        }
    }

    private static byte[] hex2byte (byte[] b, int offset, int len) {
        byte[] d = new byte[len];
        for (int i=0; i<len*2; i++) {
            int shift = i%2 == 1 ? 0 : 4;
            d[i>>1] |= Character.digit((char) b[offset+i], 16) << shift;
        }
        return d;
    }
}